<?php
    class controller_support extends __registry_controller
    {
        public $_ = array();
        public $_layout = NULL;
        public $_content_header = 'OPEN';
        public $_content_top = 'OPEN';

        public function __construct()
        {
            parent::add_object(array(
                'handle_string' ,
                'handle_upload' ,
                'handle_paging' ,
                'model_support' ,
                'handle_validate' ,
                "handle_permission",
                'model_users' ,
                'model_categories' ,
                'model_language'
            ));
            $this->_layout = NULL;

        }

        public function index_action()
        {
            $this->per_check_permission("index");
            $limit = 15;
            $res_html = NULL;
            $seo2 = isset($this->_seokey[2]) ? $this->_seokey[2] : null;
            $enseo = end($this->_seokey);
            if (empty($seo2) || ($seo2 == $enseo && intval($enseo) != 0)) {
                $this->_['url_add'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('add') . '/';
                $this->_['url_edit'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('edit') . '/';
            } else {
                $this->_['url_add'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('add') . '/' . $seo2 . '/';
                $this->_['url_edit'] = URL_BASE . 'admin/' . $this->_seokey[1] . '/' . $this->route->rt_get_seokey_act('edit') . '/' . $seo2 . '/';
            }

            $seo2 = isset($this->_seokey[2]) ? $this->_seokey[2] : null;
            $cid = isset($row['CatID']) ? $row['CatID'] : NULL;
            $this->_['CatName'] = isset($row['CatName']) ? ucwords($row['CatName']) : NULL;
            $html = $this->view->view_display('index');
            $arr_block_html = $this->view->view_get_block_html($html);

            $current = end($this->_seokey) ? intval(end($this->_seokey)) : 1;
            $total = $this->mds_support_total_list();

            $url = !empty($cid) ? URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1] . '/' . $seo2 : URL_BASE . $this->_seokey[0] . '/' . $this->_seokey[1];


            //$this->_["td_addnew"]= $arr_block_html["add_new"];
            $this->_["td_addnew"] = "";


            /*btn add-edit*/

            $this->_["btn_tool"] = $arr_block_html["btn_add"] . $arr_block_html["btn_edit"] . $arr_block_html["btn_delete"];

            /*end btn add-edit*/


            $rows = $this->mds_support_list($cid , $current , $limit);

            if (!empty($rows))
                foreach ($rows as $row) {
                    $res_html .= $this->view->view_assign($arr_block_html['product'] , $row);
                }
            $this->_['paging'] = $this->paging_display($total , $current , $limit , $url , $this->_ , ".html");
            $shtml = $this->view->view_assign_block($html , array('htmlproduct' => $res_html));
            $strOkHtml = $this->view->view_assign($shtml , $this->_);
            return $strOkHtml;
        }

        public function index_ajax_action()
        {
            $this->per_check_permission("index");

            $this->_layout = 'NO_LAYOUT';

            $active = isset($this->_seokey[3]) ? $this->_seokey[3] : null;
            $paremater = isset($this->_seokey[4]) ? $this->_seokey[4] : null;


            /*sent mail*/

            switch ($active) {
                case "order":
                    $paremater = explode("," , $paremater);
                    unset($paremater[0]);
                    foreach ($paremater as $k => $v) {
                        $this->mds_support_order($v , $k);
                    }
                    return "ok";
                    break;
                case "edit_data":
                    $res_html = "";
                    $check = "ok";
                    foreach ($_POST as $k => $value) {
                        if ($value == "")
                            $check = "NotOk";
                    }
                    if ($check == "ok") {
                        $this->mds_support_update($_POST , $paremater);
                        $rs = $this->mds_support_GetOneRow($paremater);
                        $html = $this->view->view_display('index');
                        $arr_block_html = $this->view->view_get_block_html($html);
                        $res_html .= $this->view->view_assign($arr_block_html['edit_return'] , $rs);
                        return $res_html;
                    } else {
                        return $check;
                    }
                    break;
                case "add_data":
                    $res_html = "";
                    $check = "ok";
                    foreach ($_POST as $k => $value) {
                        if ($value == "") ;
                        $check = "NotOk";
                    }


                    if ($check == "ok") {
                        $id = $this->mds_support_add($_POST);
                        $rs = $this->mds_support_GetOneRow($id);
                        $html = $this->view->view_display('index');
                        $arr_block_html = $this->view->view_get_block_html($html);
                        $res_html .= $this->view->view_assign($arr_block_html['product'] , $rs);
                        return $res_html;
                    } else {
                        return $check;
                    }
                    break;
                case "delete":
                    $rs = $this->mds_support_delete($paremater);
                    var_dump($rs);
                    break;
                case "add":
                    $html = $this->view->view_display('index');
                    $arr_block_html = $this->view->view_get_block_html($html);
                    return $arr_block_html["add_new"];
                    break;
                case "edit":
                    $html = $this->view->view_display('index');
                    $arr_block_html = $this->view->view_get_block_html($html);
                    $this->_["IDE"] = "";
                    $arr_id = explode("," , $paremater);
                    foreach ($arr_id as $key => $value) {
                        if ($value != "") {
                            $this->_["IDE"] = $value;
                            $html = $this->view->view_assign($arr_block_html["edit"] , $this->_);
                            $rs[$value] = $html;
                        }
                    }
                    return isset($rs) ? json_encode($rs) : "";
                    break;
            }
        }

    }
